package esiag.psd.cacheManager;

import java.io.Serializable;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import pds.esiag.cache.beans.MyCache;

import esiag.pds.Controlers.SizeContoler;
import esiag.pds.Controlers.expirationControler;
import esiag.pds.PoolThread.MyThreadPool;


public class CacheManager {
	private int cp;
	private MyCache objCache;
	private SizeContoler sizeControler;
	private expirationControler expirationControler;
	private Thread runner1;
	private Thread runner2;
	
	MyThreadPool poolThread;
	
	private boolean go=false;
	
	public CacheManager(int maxCacheSize,int expirationTime) {
		// TODO Auto-generated constructor stub
		cp=0;
		objCache=MyCache.getCacheInstance();
		expirationControler = new expirationControler(this,expirationTime);
		sizeControler= new SizeContoler(this,maxCacheSize);
		poolThread=new MyThreadPool(5, 5, 5000, 10);
	
		runner1=new Thread(expirationControler);
		runner2=new Thread(sizeControler);
	}
	
	public void ManagingCache(){
		if(!go){
			go=true;
			runner2.start();
			runner1.start();
		}
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		cp++;
		
	}
	
	public void stop(){
//		if (go){
//						
//		}
//		go=false;
//		cp=0;
//	//	runner1.stop();
//	//	runner2.stop();
	}
	
	public synchronized MyCache getCache(){
		return poolThread.doWork(objCache);
	}
	
	public boolean getGo(){
		return go;
	}
}
